home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / cslib15b.zip / DEMO / ADDRESS / CSADD.CPP next >
C/C++ Source or Header  |  1994-12-20  |  13KB  |  554 lines

  1. #include "ctype.h"
  2. #include "csa.h"
  3. #include "csdb.h"
  4. #include "csaddio.h"
  5.  
  6. //  Menu Options
  7.  
  8. #define mREINDEX    700
  9. #define mCREATE_DB    701
  10. #define mEXPORT_ASCII    702
  11. #define mEXPORT_DBASE    703
  12. #define mIMPORT_ASCII    704
  13.  
  14. #define mUNSORT     501
  15. #define mSORT_NAME    502
  16. #define mSORT_CITY    503
  17. #define mSORT_BIRTHDAY    504
  18. #define mSORT_RELATION    505
  19.  
  20.  
  21.  
  22.  
  23. /***********************************************************************
  24.  
  25.                                         CS Library, Release 1.5.b 
  26.  
  27.  
  28.                The CS-Library.
  29.              An address database.
  30.  
  31.                                           Copyright(c) 1994 
  32.                                           Combis 
  33.                                           The Netherlands   
  34. ***********************************************************************/
  35.  
  36.  
  37. //////////////////////////// A pop up window for messages /////////////////////
  38. WINDOW w_mess;
  39.  
  40. void message_window_clear(void) { w_mess.remove(); }
  41.  
  42.  
  43. void message_window(char *s)
  44. {
  45.      w_mess.height(10);
  46.      w_mess.width(40);
  47.      w_mess.head(" ** Message ** ");
  48.      w_mess.border(W_BORDER_DOUBLE);
  49.      w_mess.activate();
  50.      gotoxy(max(1,(40-2-(int)strlen(s))/2),4);
  51.      cputs(s);
  52. }
  53.  
  54.  
  55. ///////////////////////////////////////////////////////////////////////////
  56.  
  57.  
  58. class NAM: public NAM_foundation
  59. {
  60.  protected:
  61.  
  62.    int choice;
  63.  
  64.    MENU   m1,m2,m3,m4,m5;
  65.    PANEL  panel;
  66.  
  67.  
  68.  public:
  69.  
  70.  
  71.    void insert(void);
  72.    void def_menu(void);
  73.    int    use_menu(void)    { m1.choose(choice); return choice; }
  74.    int    use_menu(int n) { m1.find_path(n); m1.choose(choice); return choice; }
  75.  
  76.    void edit(void);
  77.    void display(void)    { panel.display(); }
  78.    void recreate(void)    { panel.recreate(); }
  79.    void create(void);
  80.  
  81. };
  82.  
  83.  
  84. /////////////////////////////////////////////////////////////////////////
  85. void NAM::insert(void)
  86. {
  87.  
  88.   append_blank();
  89.  
  90.   _update.now();
  91.  
  92.   recreate();
  93.   display();
  94.   panel.read();
  95.  
  96. }
  97.  
  98. /////////////////////////////////////////////////////////////////////////
  99. void NAM::create(void)
  100. {
  101.  
  102.   panel.remove();
  103.  
  104.   panel.set_dim(3,-1,18,65);               // Size and Position of panel
  105.   panel.head(" Address DataBase ");            // Heading
  106.   panel.border(BORDER_DOUBLE);               // Type of border
  107.   panel.activate();                   // Make panel-window visible
  108.  
  109.   gotoyx(3,10); cprintf("Name: ");
  110.   panel.add_field(4,10,25,NAME_LENGTH,rec._name);
  111.  
  112.   gotoyx(3,40); cprintf("City: ");
  113.   panel.add_field(4,40,15,CITY_LENGTH,rec._city);
  114.  
  115.   gotoyx(6,10); cprintf("Address: ");
  116.   panel.add_field(7,10,25,ADRE_LENGTH,rec._adre);
  117.  
  118.   gotoyx(6,40); cprintf("Telephone: ");
  119.   panel.add_field(7,40,15,TEL_LENGTH,rec._tel);
  120.  
  121.   gotoyx(9,10); cprintf("Zip code: ");
  122.   panel.add_field(10,10,ZIP_LENGTH,rec._zip);
  123.  
  124.   gotoyx(9,40); cprintf("Country: ");
  125.   panel.add_field(10,40,15,COUNT_LENGTH,rec._count);
  126.  
  127.  
  128.   gotoyx(1,43); cprintf("Updated: ");
  129.   panel.add_field(1,53,_update);
  130.   panel.protect(DISPLAY);
  131.  
  132.   gotoyx(12,10); cprintf("Birthday: ");
  133.   panel.add_field(13,10,_birth);
  134.  
  135.   gotoyx(12,40); cprintf("Relation: ");
  136.   panel.add_field(13,40,RELATION_LENGTH,rec._relation );
  137.  
  138.   gotoyx(15,10); cprintf("Info: ");
  139.   panel.add_field(16,10,50,INFO_LENGTH,rec._info );
  140.  
  141.   panel.exit_key(CTRL_END);
  142.   panel.escape_off();
  143.   panel.display();
  144.  
  145. }
  146.  
  147. /////////////////////////////////////////////////////////////////////////
  148. void NAM::edit(void)
  149. {
  150.  
  151.   WINDOW help;
  152.   help.set_dim( 21,-1,3,40);
  153.   help.border(W_BORDER_DOUBLE);
  154.   help.activate();
  155.   cprintf("    Use CTRL_END to exit editting.");
  156.  
  157.   panel.read();
  158.  
  159.   if(panel.changed())
  160.   {
  161.     dirty=TRUE;
  162.     _update.now();
  163.   }
  164.  
  165. }
  166. /////////////////////////////////////////////////////////////////////////
  167.  
  168. void NAM::def_menu(void)
  169. {
  170.  
  171.  
  172.  
  173.  
  174. /////////////////////////// Define the options for menu 1  //////////////
  175.  
  176.  
  177.  m1.add_option(" e~Xit "        ,ALT_X );
  178.  m1.add_option(" ~Insert "      ,ALT_I );
  179.  m1.add_option(" ~Delete "      ,ALT_D );
  180.  m1.add_option(" ~Edit "        ,ALT_E );
  181.  m1.add_option(" ~Sort order "  ,ALT_S );
  182.  m1.add_option(" ~Output "      ,ALT_O );
  183.  m1.add_option(" ~Utilities "   ,ALT_U );
  184.  m1.add_option(" Se~tup "       ,ALT_T );
  185.  m1.add_option("      ~Help=F1 ",F1 );
  186.  
  187.  
  188. /////////// Define the options for the output submenu ///////////////////////
  189.  
  190.  
  191.  m2.add_option(" P~ages " , 601 );
  192.  m2.add_option(" P~rinten ",602 );
  193.  
  194.  
  195. /////////// Define the options for the setup submenu ////////////////////////
  196.  
  197.  
  198.  m3.add_option(" ~Pack  "             ,ALT_P );
  199.  m3.add_option(" ~Rebuild Indexes "   ,mREINDEX);
  200.  m3.add_option(" ~Create new database",mCREATE_DB);
  201.  m3.add_option(" ~Export to ASCII "   ,mEXPORT_ASCII);
  202.  m3.add_option(" Export to d~BASE "   ,mEXPORT_DBASE);
  203.  m3.add_option(" ~Import from ASCII " ,mIMPORT_ASCII);
  204.  
  205. /////////// Define the options for the setup submenu ////////////////////////
  206.  
  207.  
  208.  m4.add_option(" Directories ",603 );
  209.  m4.add_option(" Colors "     ,604 );
  210.  m4.add_option(" Save Setup"  ,605 );
  211.  
  212. /////////// Define the options for the setup submenu ////////////////////////
  213.  
  214.  
  215.  m5.add_option(" ~Unsorted "          ,mUNSORT);
  216.  m5.add_option(" Sorted by ~Name "    ,mSORT_NAME);
  217.  m5.add_option(" Sorted by ~City "    ,mSORT_CITY);
  218.  m5.add_option(" Sorted by ~Relation ",mSORT_RELATION);
  219.  m5.add_option(" Sorted by ~Birthday ",mSORT_BIRTHDAY);
  220.  
  221. ///////////////// Make menu 1 'special' (main menu) ////////////////////
  222.  
  223.  
  224.   m1.type(MENU_HOR);             // Display options horizontally
  225.   m1.hold(MENU_HOLD);             // Make it 'always visible'
  226.   m1.border(BORDER_NONE);         // No border
  227.   m1.width(80);              // Set the width to the full screen
  228.  
  229.  
  230. //////////////////////////// Define the colors //////////////////////////
  231.  
  232.  
  233.   int bor_col;
  234.   int scr_col;
  235.   int opt_col;
  236.   int key_col;
  237.  
  238.  
  239.   if(is_color())
  240.   {
  241.     bor_col=make_color(WHITE,CYAN);
  242.     scr_col=make_color(WHITE,CYAN);
  243.     opt_col=make_color(WHITE,RED);
  244.     key_col=make_color(YELLOW,CYAN);
  245.   }
  246.   else
  247.   {
  248.     bor_col=make_color(LIGHTGRAY,BLACK);
  249.     scr_col=bor_col;
  250.     opt_col=make_color(BLACK,WHITE);
  251.     key_col=make_color(WHITE,BLACK);
  252.   }
  253.  
  254.  
  255. /////////////////////////////// Set the colors of each (Sub) menu /////////
  256.  
  257. //  m1.color(bor_col,make_color(YELLOW,RED),key_col,key_col);
  258.  
  259.   m1.color(bor_col,scr_col,opt_col,key_col);
  260.   m2.color(bor_col,scr_col,opt_col,key_col);
  261.   m3.color(bor_col,scr_col,opt_col,key_col);
  262.   m4.color(bor_col,scr_col,opt_col,key_col);
  263.   m5.color(bor_col,scr_col,opt_col,key_col);
  264.  
  265. ///////////////////////// Set the postions of the menu's ////////////////////
  266.  
  267.   m1.coord(1,1);
  268.   m2.relative_pos(TO_CURSOR);
  269.   m2.coord(1,1);
  270.   m3.relative_pos(TO_CURSOR);
  271.   m3.coord(1,1);
  272.   m4.relative_pos(TO_CURSOR);
  273.   m4.coord(1,1);
  274.   m5.relative_pos(TO_CURSOR);
  275.   m5.coord(1,1);
  276.  
  277. ////////////// Create the entire menu by connecting the sub-menu's //////////
  278.  
  279.   m1.connect(5,m5);
  280.   m1.connect(6,m2);
  281.   m1.connect(7,m3);
  282.   m1.connect(8,m4);
  283.  
  284.   m1.standby();
  285.  
  286. }
  287.  
  288.  
  289. /////////////////////////////////////////////////////////////////////////
  290.  
  291. void main(int argc,char *argv[])
  292. {
  293.  
  294.  
  295.    if(argc>=2 && !strcmp(argv[1],"/?"))
  296.    {
  297.  
  298.       printf("\n  * * * * * * * An address database.  * * * * * * *    ");
  299.       printf("\n\n\n CSADDRESS version 1.0.a ");
  300.       printf("\n\n Compiled at:  %s, %s ",__DATE__,__TIME__);
  301.       printf("\n\ Copyright (c) Combis ");
  302.       printf("\n\n USAGE:  csadd [/reindex] [/create] [/?] ");
  303.       printf("\n  /reindex  To rebuild the indexes. ");
  304.       printf("\n  /create   To create a new database. ");
  305.       printf("\n  /?        To display this screen.   ");
  306.       printf("\n\n");
  307.       return;
  308.    }
  309.  
  310.  
  311.    char inp[50]="";
  312.  
  313.  
  314.    NAM    nam;
  315.  
  316.  
  317.    if(argc==2 && !strcmp(argv[1],"/create"))  nam.define();
  318.    if(argc==2 && !strcmp(argv[1],"/reindex"))
  319.    {
  320.      nam.order(0);
  321.      nam.open();
  322.      nam.reindex();
  323.      nam.close();
  324.      exit(0);
  325.    }
  326.  
  327.    int    choice=-1;
  328.    int    leng;
  329.  
  330.    clrscr();
  331.  
  332.    nam.open();
  333.  
  334.  
  335.    WINDOW bckgrnd;
  336.    bckgrnd.border(BORDER_SINGLE);
  337.    bckgrnd.set_dim(2,1,23,80);
  338.    bckgrnd.activate();
  339.  
  340.    WINDOW input;
  341.    input.border(BORDER_SINGLE);
  342.    input.set_dim(21,4,3,73);
  343.    input.activate();
  344.  
  345.    nam.def_menu();
  346.  
  347.  
  348.    nam.order(NAME_INDEX);
  349.    nam.top();
  350.    nam.create();
  351.  
  352.     do
  353.     {
  354.       if(choice==-1)
  355.       {
  356.     nam.display();
  357.     input.activate();
  358.     gotoxy(5,1);
  359.     cprintf("Search Key: %s",inp); clreol();
  360.     gotoxy(50,1);
  361.     cprintf("Record %ld/%ld",nam.curr_rec(),nam.numrec());
  362.     clreol();
  363.     gotoxy(69,1);
  364.     if(nam.is_delet()) putch('D');
  365.     else           putch(' ');
  366.     gotoxy(17+strlen(inp),1);
  367.     choice=cskey();
  368.       }
  369.  
  370.       switch(choice)
  371.       {
  372.      case mUNSORT:
  373.              nam.order(UNSORTED);
  374.              nam.top();
  375.              choice=-3;
  376.              break;
  377.      case mSORT_NAME:
  378.              nam.order(NAME_INDEX);
  379.              choice=-2;
  380.              break;
  381.      case mSORT_CITY:
  382.              nam.order(CITY_INDEX);
  383.              choice=-2;
  384.              break;
  385.      case mSORT_RELATION:
  386.              nam.order(RELATION_INDEX);
  387.              choice=-2;
  388.              break;
  389.      case mSORT_BIRTHDAY:
  390.              nam.order(BIRTH_INDEX);
  391.              choice=-2;
  392.              break;
  393.      case 601:
  394.      case 602:
  395.      case 603:
  396.      case 604:
  397.      case 605:
  398.              message_window("Sorry, option not implemented.\n\rHit any key...");
  399.              waitkb(3500);
  400.              message_window_clear();
  401.              choice=-1;
  402.              break;
  403.      case mCREATE_DB:
  404.              message_window("Deleting entire database! \n\rARE YOU SURE (y/n).");
  405.              if(cskey()=='y')
  406.              {
  407.             message_window("Creating new database.");
  408.             nam.close();
  409.             nam.define();
  410.             nam.open();
  411.              }
  412.              message_window_clear();
  413.              choice=-3;
  414.              break;
  415.      case mREINDEX:
  416.              message_window("Indexing... A moment please.");
  417.              nam.reindex();
  418.              message_window_clear();
  419.              choice=-2;
  420.              break;
  421.      case mEXPORT_ASCII:
  422.              message_window("Exporting to 'backup.txt'.");
  423.              nam.export("backup.txt");
  424.              message_window_clear();
  425.              choice=-1;
  426.              break;
  427.      case mEXPORT_DBASE:
  428.              message_window("Exporting to 'DBASE.dbf'.");
  429.              nam.to_DBASE("DBASE.dbf");
  430.              message_window_clear();
  431.              choice=-1;
  432.              break;
  433.      case mIMPORT_ASCII:
  434.              message_window("Importing from 'backup.txt'.");
  435.              nam.import("backup.txt");
  436.              message_window_clear();
  437.              choice=-3;
  438.              break;
  439.      case F1:
  440.      case ALT_H:
  441.              {
  442.             WINDOW help;
  443.             help.set_dim(-1,-1,14,50);
  444.             help.head(" Help Screen ");
  445.             help.activate();
  446.  
  447.             cprintf("\n                 Main Keys ");
  448.             cprintf("\n\r");
  449.             cprintf("\n\r    Edit:     ALT-E    ");
  450.             cprintf("\n\r    End Edit: CTRL-END ");
  451.             cprintf("\n\r");
  452.             cprintf("\n\r    Insert:   ALT-I ");
  453.             cprintf("\n\r    Delete:   ALT-D ");
  454.             cprintf("\n\r    Exit:     ALT-X ");
  455.             cprintf("\n\r    Menu:     F10   ");
  456.             cprintf("\n\r    Clear search key: ESC");
  457.             cprintf("\n\n\r              Hit any key");
  458.             cskey();
  459.  
  460.              }
  461.              choice=-1;
  462.              break;
  463.      case ALT_D:
  464.              if(nam.is_delet()) nam.undelet();
  465.              else        nam.delet();
  466.              choice=-1;
  467.              break;
  468.      case ALT_E:
  469.              nam.edit();
  470.              choice=-3;
  471.              break;
  472.      case ALT_I:
  473.              nam.insert();
  474.              choice=-3;
  475.              break;
  476.      case ALT_P:
  477.              message_window("Packing... A moment please.. ");
  478.              nam.pack();
  479.              message_window_clear();
  480.              nam.top();
  481.              choice=-3;
  482.              break;
  483.      case ALT_X:
  484.              break;
  485.      case ALT_O:
  486.      case ALT_S:
  487.      case ALT_T:
  488.      case ALT_U:
  489.              choice=nam.use_menu(choice);
  490.              break;
  491.      case F10:
  492.              choice=nam.use_menu();
  493.              break;
  494.      case CURSOR_UP:
  495.              nam.skip(-1);
  496.              choice=-3;
  497.              break;
  498.      case CURSOR_DOWN:
  499.              nam.skip(1);
  500.              choice=-3;
  501.              break;
  502.      case PAGE_DOWN:
  503.              nam.skip(10);
  504.              choice=-3;
  505.              break;
  506.      case PAGE_UP:
  507.              nam.skip(-10);
  508.              choice=-3;
  509.              break;
  510.      case HOME:
  511.              nam.top();
  512.              choice=-3;
  513.              break;
  514.      case END:
  515.              nam.bottom();
  516.              choice=-3;
  517.              break;
  518.      case BACKSPACE:
  519.              if(strlen(inp))
  520.              {
  521.                inp[strlen(inp)-1]=0;
  522.                choice=-2;
  523.              }
  524.              else choice=-1;
  525.              break;
  526.      case ESC:
  527.              inp[0]=0;
  528.      case -2:
  529.              nam.search(inp);
  530.      case -3:
  531.              nam.recreate();
  532.              choice=-1;
  533.              break;
  534.      default:
  535.              if(isprint(choice))
  536.              {
  537.             leng=strlen(inp);
  538.             inp[leng]=choice;
  539.             inp[leng+1]=0;
  540.             choice=-2;
  541.              }
  542.              else choice=-1;
  543.              break;
  544.       }
  545.  
  546.     } while(choice!=ALT_X);
  547.  
  548.  
  549.     win_remove_all();
  550.     nam.close();
  551.  
  552. }
  553.  
  554.